using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
using Fireball.Syntax.SyntaxDocumentExporters;

namespace AjGenesisStudio.Commands
{
    class ExportHtmlCommand : Command
    {

        public override bool Execute()
        {
            return Execute(Global.Instance.MainForm.GetActiveDocument());
        }

        public bool Execute(Documents.Document doc)
        {
            if (doc == null)
                return true;

            Global.Instance.StartWork("Exporting as html");

            string path = ShowSave();
            if (path != string.Empty)
            {
                HTMLExporter exp = new HTMLExporter();
                string html = exp.Export(doc.syntaxdoc, string.Empty);
                html = html.Replace("<img src=\"clear.gif\"  align=top>", string.Empty);
                StreamWriter writer = File.CreateText(path);
                writer.Write(html);
                writer.Close();
            }
            
            Global.Instance.EndWork();

            return true;
        }

        public static string ShowSave()
        {
            string filters = string.Empty;
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.DefaultExt = "html";
            filters = "HTML File (*.htm)|*.htm|";
            dlg.Filter = filters + "Any file (*.*)|*.*";

            if (dlg.ShowDialog() == DialogResult.OK)
                return dlg.FileName;
            else
                return string.Empty;
        }
    }
}
